package com.ego.item.controller;

import com.ego.item.pojo.Category;
import com.ego.item.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
@CrossOrigin
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 根据pid获取分类信息
     * @param pid
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity<List<Category>> list(@RequestParam("pid") Long pid){
        List<Category> result = categoryService.findListByPid(pid);
        if (result == null){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(result);
    }

    /**
     * 保存新增分类信息
     * @param category
     * @return
     */
    @PostMapping
    //@RequestBody  用于接收json对象
    public ResponseEntity<Void> saveCategory(@RequestBody Category category){
        categoryService.save(category);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 更新分类信息
     * @param id
     * @param name
     * @return
     */
   @PutMapping
    public ResponseEntity<Void> update(@RequestParam(value = "id") Long id,
                                       @RequestParam(value = "name") String name){
        categoryService.updateNameById(id,name);
        return ResponseEntity.ok().build();
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> delete(@PathVariable("id") Long id){
        categoryService.deleteById(id);
        return ResponseEntity.ok().build();
    }

    /**
     * 修改品牌信息
     * URL：http://api.ego.com/api/item/category/bid/325406
     * 参数：bid(品牌id)
     */
    @GetMapping("/bid/{bid}")
    public ResponseEntity<List<Category>> queryCategoryList(@PathVariable Long bid){
       //1,先查询商品分类信息进行回显
       List<Category> result = categoryService.findListByBid(bid);
       return ResponseEntity.ok().build();
    }

}
